<template>
  <div>
    <div class="image-info" @click="openCropper()" :style="fixedBox ? {width: cropWidth + 'px', height: cropHeight + 'px', lineHeight: cropHeight + 'px'} : {maxHeight: '150px', maxWidth: '280px', lineHeight: '150px'}">
      <img class="" v-bind:src="options.img" title="点击上传图片" :style=" fixedBox ? {width: cropWidth + 'px', height: cropHeight + 'px'} : {maxHeight: '150px', maxWidth: '280px'}"/>
    </div>
    <el-button size="mini" type="primary" @click="handleSelectFile">从文件管理选择</el-button>
    <!-- 图片裁剪 -->
    <base-image-cropper
      v-if="visible"
      :visible.sync="visible"
      :image-url="imageUrl"
      :file-path="filePath"
      @afterUpload="setImageUrl"
      :fixed-box="fixedBox"
      :crop-width="cropWidth"
      :crop-height="cropHeight"
    />
    <!-- 文件管理  -->
    <file-select :visible.sync="showFileSelect" :file-ext-name="fileType"  @setFileUrl="setImageUrl"/>
  </div>
</template>

<script>
import BaseImageCropper from '@/components/BaseImageCropper/index.vue'

export default {
  components: { BaseImageCropper },
  props: {
    imageUrl: {
      type: String,
      default: ''
    },
    cropWidth: {
      type: Number,
      default: 200
    },
    cropHeight: {
      type: Number,
      default: 200
    },
    filePath: {
      type: String,
      default: '/blog/mage-cropper'
    },
    fixedBox: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      // 是否显示cropper
      visible: false,
      // 弹出层标题
      title: "修改图片",
      options: {
        // 裁剪图片的地址
        img: this.imageUrl
      },
      previews: {},
      // 文件名
      fileName: '',
      // 显示文件管理
      showFileSelect: false,
      // 文件类型
      fileType: ["png", "jpg", "jpeg", 'blob']
    };
  },
  methods: {
    // 打开裁剪弹框
    openCropper() {
      this.visible = true;
    },
    setImageUrl(url) {
      this.$emit('setImageUrl', url)
      this.options.img = url
      this.showFileSelect = false
      this.visible = false
    },
    handleSelectFile() {
      this.showFileSelect = true
    }
  }
};
</script>
<style scoped lang="scss">
.image-info {
  position: relative;
  display: block;
  border: 0.5px solid #aaa;
  border-radius: 5px;
  overflow: hidden;
}

.image-info:hover:after {
  content: '+';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  color: #eee;
  background: rgba(0, 0, 0, 0.5);
  font-size: 24px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  cursor: pointer;
  text-align: center;
}
.image-upload-preview {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 1px #ccc;
  overflow: hidden;
}
</style>
